package net.hwii.obj;

import java.util.List;

import net.hiwii.util.StringUtil;
import net.hwii.core.Block;
import net.hwii.core.Transaction;

public class LocalNode {
	public static class Bucket{
	    private int k; //当前是第几个k桶
	    private Bucket next;//下一个k桶
	}
	public List<Transaction> getTransactions(){
		return null;
	}
	
	public boolean judgeTransanction(Transaction txn) {
		return true;
	}
	
	public List<Node> getLocalNodes(){
		return null;
	}
	
	public List<Node> getNodesList(Node node){
		return null;
	}
	
	public long getLocalTime() {
		return 0;
	}
	
	public long getTimeStamp(Node node) {
		return 0;
	}
	//Calculate new hash based on blocks contents
	public String calculateHash(Block block) {
		String calculatedhash = StringUtil.applySha256( 
				block.getPreviousHash() +
				Long.toString(block.getTimeStamp()) +
				Integer.toString(block.getNonce()) + 
				block.getData() 
				);
		return calculatedhash;
	}

	//Increases nonce value until hash target is reached.
	public void mineBlock(Block block, int difficulty) {
		String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0" 
		while(!block.getHash().substring( 0, difficulty).equals(target)) {
			block.setNonce(block.getNonce() + 1);
			block.setHash(calculateHash(block));
		}
		System.out.println("Block Mined!!! : " + block.getHash());
	}
}
